package com.csxy.systemmanager.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csxy.systemmanager.dao.UserDao;
import com.csxy.systemmanager.entity.LoginUser;
import com.csxy.systemmanager.common.ResponseResult;
import com.csxy.systemmanager.entity.User;
import com.csxy.systemmanager.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;


@Service("UserService")
public class UserServiceImpl extends ServiceImpl<UserDao,User> implements UserService {
    //注入dao层
    @Autowired
    private UserDao userDao;
    @Override
    public ResponseResult getCurrentUser() {
        //拿到当前用户
        UsernamePasswordAuthenticationToken authentication = (UsernamePasswordAuthenticationToken)SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        return new ResponseResult(200,"ok",loginUser.getUser());
    }


    @Override
    public IPage<User> getUserListByPage(Integer current, Integer size, String username) {
        Page<User> page = new Page<>(current, size);
        return userDao.queryByPage(page, username);
    }

    @Override
    public ResponseResult addUser(User user) {
        //新增用户
        userDao.insert(user);
        return new ResponseResult(200,"ok");
    }
}
